package com.cloudsigns.factorymanager2.project.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.project.adapter.ProductInformationAdapter;
import com.cloudsigns.factorymanager2.project.bean.ProductBean;
import com.cloudsigns.factorymanager2.project.customview.WorkSheetCategoryPoupWindow;
import com.cloudsigns.factorymanager2.project.customview.WorkSheetLevelPopupWindow;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 生产任务单
 *
 */

public class ProductionTaskOrderActivity extends Activity implements View.OnClickListener,ItemRemoveListener{
    private ImageView img_back;
    private TextView tv_title;
    private EditText edt__project_name;
    private ImageView img_add_product;
    private TextView edt__work_sheet_category, edt__level;
    private TextView btn_save;
    private WorkSheetCategoryPoupWindow workSheetPopupwindow;
    private WorkSheetLevelPopupWindow workSheetLevelPopupWindow;
    private String workSheetCategoryStr, worksheetLevelStr;
    private ListView lv;
    private ProductInformationAdapter adapter;
    private List<ProductBean> productBeanList = new ArrayList<>();
    private final String TAG = "ProductionTaskOrderActivity";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    );
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);

        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_production_task_order);

        init();
    }

    private void init() {
        img_back = findViewById(R.id.img_back);
        tv_title = findViewById(R.id.tv_title);
        edt__work_sheet_category = findViewById(R.id.edt__work_sheet_category);
        edt__level = findViewById(R.id.edt__level);
        img_add_product = findViewById(R.id.img_add_product);
        btn_save = findViewById(R.id.btn_save);
        lv = findViewById(R.id.lv);

        tv_title.setText(getResources().getText(R.string.production_task_order));
        img_back.setOnClickListener(this);
        edt__work_sheet_category.setOnClickListener(this);
        edt__level.setOnClickListener(this);
        img_add_product.setOnClickListener(this);
        btn_save.setOnClickListener(this);

        adapter = new ProductInformationAdapter(this, TAG);
        adapter.setRemoveListener(this);
        adapter.setData(productBeanList);
        lv.setAdapter(adapter);

    }

    private void showWorkSheetCategoryPoupWindow(View anchor) {
        workSheetPopupwindow = new WorkSheetCategoryPoupWindow(this);
        workSheetPopupwindow.setOnClickListener(this);
        workSheetPopupwindow.showPopupWindow(anchor);
    }

    private void showLeverPopupWindow(View anchor) {
        workSheetLevelPopupWindow = new WorkSheetLevelPopupWindow(this);
        workSheetLevelPopupWindow.setOnClickListener(this);
        workSheetLevelPopupWindow.showPopupWindow(anchor);
    }

    private void save() {
        Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (workSheetPopupwindow != null) {
            workSheetPopupwindow.dismiss();
            workSheetPopupwindow = null;
        }

        if (workSheetLevelPopupWindow != null) {
            workSheetLevelPopupWindow.dismiss();
            workSheetLevelPopupWindow = null;
        }
    }

    @Override
    public void remove(int positon) {  //列表中删除事件
        if (positon >= 0 && positon < productBeanList.size()) {
            productBeanList.remove(positon);
            adapter.setData(productBeanList);
            adapter.notifyDataSetChanged();
        }

    }

    @Override
    public void remove(View view) {

    }

    @Override
    public void onClick(View view) {
        TextView tv;
        Intent mIntent;
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.edt__work_sheet_category:
                showWorkSheetCategoryPoupWindow(view);
                break;
            case R.id.img_add_product:
                mIntent = new Intent(ProductionTaskOrderActivity.this, AddProductInfomationActivity.class);
                startActivityForResult(mIntent, ConstantUtil.ADD_PRODUCT_INFOMATION);
                break;
            case R.id.btn_save:
                save();
                break;
            case R.id.tv_A:
            case R.id.tv_B:
            case R.id.tv_C:
            case R.id.tv_D:

                tv = (TextView) view;
                workSheetCategoryStr = tv.getText().toString().trim();
                edt__work_sheet_category.setText(workSheetCategoryStr);
                if (workSheetPopupwindow != null)
                    workSheetPopupwindow.dismiss();
                break;
            case R.id.tv_first_level:
            case R.id.tv_second_lever:
            case R.id.tv_third_level:
            case R.id.tv_firth_lever:
                tv = (TextView) view;
                worksheetLevelStr = tv.getText().toString().trim();
                edt__level.setText(worksheetLevelStr);
                if (workSheetLevelPopupWindow != null)
                    workSheetLevelPopupWindow.dismiss();

                break;
            case R.id.edt__level:
                showLeverPopupWindow(view);
                break;

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case ConstantUtil.ADD_PRODUCT_INFOMATION: //添加产品信息
                if (data != null) {
                    boolean bool = data.getBooleanExtra(ConstantUtil.PRODUCTBEAN, false);
                    if (bool) {
//                        productBeanList.add(new ProductBean(null, 0, null, null, 0, null));
                        adapter.setData(productBeanList);
                        adapter.notifyDataSetChanged();
                    }
//                    ProductBean productBean = (ProductBean) data.getSerializableExtra(ConstantUtil.PRODUCTBEAN);
//                    productBeanList.add(productBean);
//                    adapter.notifyDataSetChanged();
                }
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
